package com.chaopei.msite_web.utils;

import com.chaopei.goods_mng_api.entity.Product;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Corday 天涯
 * @Date 2022/5/10 15:05
 * redis数据库的管理
 */
@Component
public class MyRedis {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 存入redis
     *
     * @param key
     * @param json:value
     */
    public void setString(String key, String json) {
        ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
        vo.set(key, json);
    }

    /**
     * 从redis拿到集合对象
     *
     * @return
     */
    public List<Product> getProducts(String key) {
        List<Product> products = null;
        ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
        //获取redis中的数据,一定要做判断
        String json = vo.get(key);
        //可能判断json.equals("error")
        if (json != null) {
            //products=new ArrayList<>();
            products = MyTools.getProducts(json);
        }
        return products;
    }
}
